package action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import biz.DepartBiz;
import biz.EmployeeBiz;
import entity.Depart;
import entity.Employee;

/**
 * @author 陈彬彬
 * @publish 2015-11-17 下午10:47:58
 * @category 员工控制层类
 */
@Controller
public class EmployeeAction extends BaseAction {
	@Resource
	private EmployeeBiz biz;
	@Resource
	private DepartBiz dbiz;
	
	private Employee emp;
	private List<Employee> emps;
	private List<Depart> deps;
	private String code;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Employee getEmp() {
		return emp;
	}
	public void setEmp(Employee emp) {
		this.emp = emp;
	}
	public List<Employee> getEmps() {
		return emps;
	}
	public void setEmps(List<Employee> emps) {
		this.emps = emps;
	}
	public List<Depart> getDeps() {
		return deps;
	}
	public void setDeps(List<Depart> deps) {
		this.deps = deps;
	}
	
	public String login(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		if(!code.equals(session.getAttribute("code"))){
			setMsg("验证码错误或过期，请重试");
			return "nologin";
		}
		emp=biz.login(emp.getLoginid(), emp.getPwd());
		if(emp==null){
			setMsg("用户名或密码错误！");
			return "nologin";
		}
		session.setAttribute("user", emp);
		return "login";
	}
	public String exit(){
		HttpSession session=ServletActionContext.getRequest().getSession();
		session.invalidate();
		return "nologin";
	}
	
		//增
		public String add(){
			if(biz.add(emp)){
				this.setMsg("添加员工成功");		
				emp=null;
			}else{
				this.setMsg("添加员工失败");		
			}
			return data();
		}
		//删
		public String del(){
			if(biz.del(getId())){
				this.setMsg("删除员工成功！");
			}else{
				this.setMsg("删除员工失败！");
			}
			return list();
		}
		//改
		public String update(){
			if(biz.update(emp)){
				this.setMsg("更新员工信息成功！");
				return list();
			}else{
				this.setMsg("更新员工信息失败！");
				return data();
			}
		}
		//查
		public String list(){
			emps=biz.queryByPage(getPage(), 5);
			int count=biz.count();
			setTpage(count%5==0?count/5:(count/5+1));
			return "list";
		}
		
		public String data(){
			deps=dbiz.query();
			return "edit";
		}
		public String show(){
			emp=biz.get(getId());
			if(emp==null){
				setMsg("该员工不存在！");
				return list();
			}
			return data();
		}
}
